Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance iDynTree from / to Python mapping and add proper NumPy support #726

Merged
merged 11 commits into from
Sep 1, 2020

Conversation

diegoferigo
Copy link
Member

After only one year from #484 (comment) I finally managed to fix and refactor that PR.

I don't know if it is a wise choice, but I tried to maintain the same APIs that @francesco-romano developed at that time. I implemented a somehow cleaner version of the original pull requests that 1) does not require to write any low-level CPython code and 2) exploits the existing typemaps provided by numpy.i. I cherry picked the original Python test.

Particularly, I exploited 2) in such a way that the memory of the objects mapped to numpy is handled by Python. When an object gets deleted, its memory is buffered for being garbage-collected [3].

Contrarily to #484, I used a mixture of C++ wrappers functions and Python extend in the SWIG file. Luckily, the recent refactoring and new inheritance scheme of #704 (see #704 (comment) for more details) helped reducing the cases.


Resources

@CLAassistant
Copy link

CLAassistant commented Aug 27, 2020

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ diegoferigo
❌ francesco-romano
You have signed the CLA already but the status is still pending? Let us recheck it.

@traversaro
Copy link
Member

Luckily, the recent refactoring and new inheritance scheme of #704 (see #704 (comment) for more details) helped reducing the cases.

This PR is targeting master, where #704 did not landed for now.

@traversaro
Copy link
Member

Tests on Ubuntu 18.04 are failing with:

 [ 98%] Building CXX object bindings/python/CMakeFiles/_iDynTree_python.dir/CMakeFiles/_iDynTree_python.dir/iDynTreePYTHON_wrap.cxx.o
/home/runner/work/idyntree/idyntree/build/bindings/python/CMakeFiles/_iDynTree_python.dir/iDynTreePYTHON_wrap.cxx:5515:10: fatal error: numpy/arrayobject.h: No such file or directory
 #include <numpy/arrayobject.h>
          ^~~~~~~~~~~~~~~~~~~~~

Based on https://packages.ubuntu.com/search?searchon=contents&keywords=numpy%2Farrayobject.h&mode=exactfilename&suite=bionic&arch=any I guess we need to either install python3-numpy, or pass some additional include directories to the compilation of the Python bindings.

if(NOT IDYNTREE_USES_PYTHON_VERSION)
set (IDYNTREE_USES_PYTHON_VERSION ${PYTHON_VERSION_STRING})
endif()
find_package(Python3 ${IDYNTREE_USES_PYTHON_VERSION} COMPONENTS Interpreter Development REQUIRED)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as this is targeting devel (i.e. iDynTree 2), it is ok to just support Python3 . In this context, what is the value of IDYNTREE_USES_PYTHON_VERSION ? Permit to find for different Python 3 version? I am not sure if FindPython3 works in this way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can ignore Python2 support, I'll remove IDYNTREE_USES_PYTHON_VERSION entirely. If you want to find a specific version, you can use find_package(Python3 3.6 EXACT [...]), however this is not the case since we just need any Python3. Done in e6b6f50.

get_filename_component(PYTHON_DIR ${PYTHON_LIBRARY} PATH)
link_directories(${PYTHON_DIR})
if(NOT IDYNTREE_USES_PYTHON_VERSION)
set (IDYNTREE_USES_PYTHON_VERSION ${Python3_VERSION})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As IDYNTREE_USES_PYTHON_VERSION is a cache variable, this is either not doing anything, or in any case something of really confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, in light of also #726 (comment) I removed it.

@traversaro
Copy link
Member

Tests on Ubuntu 18.04 are failing with:

 [ 98%] Building CXX object bindings/python/CMakeFiles/_iDynTree_python.dir/CMakeFiles/_iDynTree_python.dir/iDynTreePYTHON_wrap.cxx.o
/home/runner/work/idyntree/idyntree/build/bindings/python/CMakeFiles/_iDynTree_python.dir/iDynTreePYTHON_wrap.cxx:5515:10: fatal error: numpy/arrayobject.h: No such file or directory
 #include <numpy/arrayobject.h>
          ^~~~~~~~~~~~~~~~~~~~~

Based on https://packages.ubuntu.com/search?searchon=contents&keywords=numpy%2Farrayobject.h&mode=exactfilename&suite=bionic&arch=any I guess we need to either install python3-numpy, or pass some additional include directories to the compilation of the Python bindings.

Checking a bit more the docs, I think we search and link the Python3::NumPy imported target ( https://cmake.org/cmake/help/v3.14/module/FindPython3.html#imported-targets ).

@diegoferigo diegoferigo changed the base branch from master to devel August 30, 2020 10:02
@diegoferigo
Copy link
Member Author

diegoferigo commented Aug 30, 2020

Thanks @traversaro for the feedback!

This PR is targeting master, where #704 did not landed for now.

You're right, I rebased the PR on devel. I actually think that it could work also on master as it is, but if we can ignore python2 targeting devel maybe it's better doing so.

I think we search and link the Python3::NumPy imported target

I tried to compile and run the tests in my setup (bionic + virtualenv) and I didn't need to link that target. You're right that it's missing, maybe my system has those headers installed with apt in a already included directory. Done in 1d89ebe.

@traversaro
Copy link
Member

Sorry, devel was behind master, and that is the reason why CI was failing (devel was missing #725), if you rebase on the top of latest devel everything should work fine.

@diegoferigo
Copy link
Member Author

Thanks, rebased. Let's wait CI.

@diegoferigo
Copy link
Member Author

@traversaro How should we handle the CLA? Either @francesco-romano signs it or I substitute the cherry-picked commit.

@traversaro
Copy link
Member

I will force the check, as Francesco Romano already signed the CLA in another form.

@traversaro
Copy link
Member

Can you update the CHANGELOG with the changes contained in this PR? Main thing to mention: dropped Python2 support, removed iDynTree.init_helpers()/iDynTree.init_numpy_helpers(), fromPyList, new features added.

@diegoferigo
Copy link
Member Author

Updated the changelog in 0ad3c61.

I noticed that the dyncomp.py test is never added to the test suit, and it uses old classes that no longer exists and its syntax is Python2. I plan to use iDynTree bindings to create some high(er) level wrappers for KinDynComputations and InverseKinematics, let's see where to add the tests, either here or downstream.

@traversaro
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants